from selenium.webdriver.common.by import By
from common.common import driver
import time

#注册测试
class TestSignup:
    url=""
    driver=""

    def __init__(self):
        self.driver=driver.driver
        self.url="http://127.0.0.1:58080/sign-up.html"
        # 打开注册网页
        self.driver.get(self.url)

    # 正确注册用例
    def Sucsignup(self):
        # 添加屏幕截图
        driver.getScreeShot()
        # 清空所有输入框
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()

        # 输入用户名
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("wyreee")
        # 输入昵称
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("wyreee")
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        # 输入确认密码
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123")
        # 勾选同意政策
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        # 点击注册按钮
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 找到用户登录四个字即注册成功
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR,"body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2")
        # 添加屏幕截图
        driver.getScreeShot()
        # 返回注册界面,点击登录界面下的点击注册
        self.driver.find_element(By.CSS_SELECTOR,"body > div > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a").click()
        # 点击下方登录按键，是否跳转登陆页面
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page.page-center > div > div.text-center.text-muted.mt-3 > a").click()
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2").text
        assert text == "用户登录"

    #错误注册用例
    def FailSignup(self):
        # 清空所有输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()

        # 只填写用户名，不同意政策，不点击注册按钮
        # 输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("wyr1234")
        # 未跳转登录界面，找到用户注册四个字即未注册成功
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > h2")
        # 添加屏幕截图
        driver.getScreeShot()

        # 只填写密码和确认密码，不同意政策，点击注册按钮
        # 清空输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # 输入确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123")
        # 点击注册按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 未跳转登录界面，找到用户注册四个字即未注册成功
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > h2")
        # 添加屏幕截图
        driver.getScreeShot()

        # 只填写昵称和确认密码，不同意政策，点击注册按钮
        # 清空输入框
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()
        # 输入昵称
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("wyr1234")
        # 输入确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123")
        # 点击注册按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 未跳转登录界面，找到用户注册四个字即未注册成功
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > h2")
        # 添加屏幕截图
        driver.getScreeShot()

        # 只填写密码，同意政策，不点击注册按钮
        # 清空输入框
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # 勾选同意政策
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        # 未跳转登录界面，找到用户注册四个字即未注册成功
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > h2")
        # 添加屏幕截图
        driver.getScreeShot()

        # 只填写用户名、昵称、密码，不同意政策，点击注册按钮
        # 清空输入框
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        #取消勾选同意政策
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        # 输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("wyr1234")
        # 输入昵称
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("wyr1234")
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # 点击注册按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 未跳转登录界面，找到用户注册四个字即未注册成功
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > h2")
        # 添加屏幕截图
        driver.getScreeShot()

        # 只填写用户名和确认密码，同意政策，不点击注册按钮
        # 清空输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("wyr1234")
        # 输入确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123")
        # 勾选同意政策
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        # 未跳转登录界面，找到用户注册四个字即未注册成功
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > h2")
        # 添加屏幕截图
        driver.getScreeShot()

        # 只填写昵称，同意政策，不点击注册按钮
        # 清空输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()
        # 输入昵称
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("wyr1234")
        # 未跳转登录界面，找到用户注册四个字即未注册成功
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > h2")
        # 添加屏幕截图
        driver.getScreeShot()

        # 填写已有的账号，点击注册
        # 清空所有输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()
        # 输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("wyr")
        # 输入昵称
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("wyr")
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # 输入确认密码
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123")
        # 勾选同意政策
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        # 点击注册按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 弹出警告弹窗
        time.sleep(0.5)
        text=self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div > h2").text
        assert text == "警告"
        # 添加屏幕截图
        driver.getScreeShot()
